package Modelo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Observable;

import Interfaces.*;


public class Modelo extends Observable
{
	private InterfaceCalcular _interfaceCalcular;
	private InterfacePrograma _interfacePrograma;
	private InterfaceMedia _interfaceMedia;
	private InterfaceSomatorio _interfaceSomatorio;
	private InterfacePrincipal _interfacePrincipal;
	
	private File f;
	
	private Equation[] formulas;
	
	public Modelo()
	{
		//Inicializa asequa��es. Aqui vai ficar o load do ficheiro.
		formulas = new Equation[18];	
		
		f = new File("formulas.txt");
	}
	public InterfaceCalcular getInterfaceCalcula() {
		return _interfaceCalcular;
	}
	public void setInterfaceCalcular(InterfaceCalcular interfaceCalcular) {
		this._interfaceCalcular = interfaceCalcular;
	}
	public InterfacePrograma getInterfacePrograma() {
		return _interfacePrograma;
	}
	public void setInterfacePrograma(InterfacePrograma interfacePrograma) {
		this._interfacePrograma = interfacePrograma;
	}
	public InterfaceMedia getInterfaceMedia() {
		return _interfaceMedia;
	}
	public void setInterfaceMedia(InterfaceMedia interfaceMedia) {
		this._interfaceMedia = interfaceMedia;
	}
	public InterfaceSomatorio getInterfaceSomatorio() {
		return _interfaceSomatorio;
	}
	public void setInterfaceSomatorio(InterfaceSomatorio interfaceSomatorio) {
		this._interfaceSomatorio = interfaceSomatorio;
	}
	public InterfacePrincipal getInterfacePrincipal() {
		return _interfacePrincipal;
	}
	public void setInterfacePrincipal(InterfacePrincipal interfacePrincipal) {
		this._interfacePrincipal = interfacePrincipal;
	}

	public Erros adicionaEq(String infix, int bt) throws FileNotFoundException, IOException
	{
		Equation formula = new Equation(infix);
		
		Erros idValidacao = formula.criarPostfix();
		
		if(idValidacao != Erros.VALIDO)
			return idValidacao;
		
		formulas[bt] = formula;
		
		return Erros.VALIDO;
	}
	
	public void saveFormulas() throws IOException
	{
		if(f.exists())
		{
			f.delete();
		    f.createNewFile();
		}
	
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
		
		oos.writeObject(formulas);
		oos.flush();
		oos.close();
	}
	
	public void loadFormulas() throws FileNotFoundException, IOException, ClassNotFoundException
	{
		try {
			if(f.exists())
			{
				ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
				
				formulas = (Equation[])ois.readObject();
				
				ois.close();
			}
		} catch (StreamCorruptedException e) {
			//ignora load
		}
	}
	
	public String getFormula(int bt)
	{
		if(formulas[bt] == null)
			return "Sem fórmula.";
		
		return formulas[bt].getInfix();
	}
	
	public void limpaFormula(int bt)
	{
		formulas[bt] = null;
	}
	
	public boolean isVazio(int bt)
	{
		if(formulas[bt] == null)
			return true;
		else 
			return false;
	}
	
	public int tamanhoVars(int bt)
	{
		return formulas[bt].tamanhoVars();
	}
	
	
	public ArrayList<String> getVariaveis(int bt)
	{
		return formulas[bt].getVariaveis();
	}
	
	
	public void setVar(int bt, double value, String var)
	{
		formulas[bt].setVar(value, var);
	}
	
	public String getEqNome(int bt)
	{
		if(formulas[bt] == null || formulas[bt].getNome().length() == 0)
			return "F" + (bt + 1);
		
		return formulas[bt].getNome();
	}
	
	public boolean setEqNome(String nome, int bt)
	{
		if(formulas[bt] == null)
			return false;
		
		formulas[bt].setNome(nome);
		
		return true;
	}
	
	public double resolverEq(int bt)
	{
		Equation formula = formulas[bt];
				
		return formula.resolver();
	}
	
	public void preparaFormula(int bt)
	{
		formulas[bt].criarArvore();
	}
}